/*
  standard  基本信息
  姓名 name
  性别 gender
  生日 birthday
  工作经验 career - number/string
  地址  address - {province, city, district, detail}
  自我介绍 desc

  contact 联系方式
  phone
  email

  experiences 工作经验 []
  一条工作经验里的数据
  公司 company
  职位 title
  时间 time {start: {year, month}, end?: {year, month}}
  管理岗位 manager {isManager: true, superior, teamMembers}

  educations 教育经历 []
  学校 school
  专业 major
  时间 time {start: {year, month}, end?: {year, month}}
  学位 degree
*/

import { useState } from 'react'
import Block from './Block'
import Standard from './Standard'
import Contact from './Contact'
import Experiences from './Experiences'

const Resume = ({
  data: { standard, contact, experiences = [], educations = [] },
  onUpdate,
}) => {
  const [isEdit, setIsEdit] = useState({})

  return (
    <>
      <h1>我的简历</h1>
      {/* 基本信息 */}
      <Block
        title={'基本信息'}
        action="编辑"
        data={standard}
        isEdit={isEdit.standard}
        component={Standard}
        onAction={() => {
          setIsEdit({ ...isEdit, standard: true })
        }}
        onSubmit={(data) => {
          onUpdate({ standard: data })
          setIsEdit({ ...isEdit, standard: false })
        }}
        onCancel={() => {
          setIsEdit({ ...isEdit, standard: false })
        }}
      />
      {/* 联系方式 */}
      <Block
        title="联系方式"
        action="编辑"
        data={contact}
        isEdit={isEdit.contact}
        component={Contact}
        onAction={() => {
          setIsEdit({ ...isEdit, contact: true })
        }}
        onSubmit={(data) => {
          onUpdate({ contact: data })
          setIsEdit({ ...isEdit, contact: false })
        }}
        onCancel={() => {
          setIsEdit({ ...isEdit, contact: false })
        }}
      />
      {/* 工作经历 */}
      <Block
        title={'工作经历'}
        action="添加"
        data={experiences}
        isEdit={isEdit.experiences}
        component={Experiences}
        onAction={() => {
          console.log('*****')
          setIsEdit({ ...isEdit, experiences: true })
        }}
        onSubmit={(action, index, item) => {
          const copy = [...experiences]
          if (action === 'remove') {
            copy.splice(index, 1)
          } else {
            if (index === -1) {
              copy.push(item)
            } else {
              copy.splice(index, 1, item)
            }
          }
          onUpdate({ experiences: copy })
          setIsEdit({ ...isEdit, experiences: false })
        }}
        onCancel={() => {
          setIsEdit({ ...isEdit, experiences: false })
        }}
      />

      {/* 教育经历 */}
    </>
  )
}

export default Resume
